<?php
/**
 * 考场管理
 * @since   2018-06-14
 * @author  winljm001 <winljm001@126.com>
 */

namespace app\admin\controller;



use app\model\AdminTestPersonnel;
use app\util\ReturnCode;
use app\util\Tools;

class TestPersonnel extends Base {

    /**
     * 获取考试人员列表
     * @return array
     * @throws \think\exception\DbException
     * @author  winljm001 <winljm001@126.com>
     */
    public function index() {

        $limit = $this->request->get('size', config('apiAdmin.ADMIN_LIST_DEFAULT'));
        $start = $this->request->get('page', 1);
        $keywords = $this->request->get('keywords', '');
        $status = $this->request->get('status', '');
        $startTime = $this->request->get('startTime', '');
        $endTime = $this->request->get('endTime', '');
        $where = [];
        if($startTime!=''){
            $where['examStartTime'] = ['gt', $startTime];
        }
        if($endTime!=''){
            $where['examEndTime'] = ['lt', $endTime];
        }
        if ($status === '1' || $status === '0') {
            $where['status'] = $status;
        }
        
        
        if($keywords!=''){
            $where=array_merge($where,['id|name'=>['like', "%{$keywords}%"]]);
        }

        $listObj = (new AdminTestPersonnel())->where($where)->order('updateTime DESC')
            ->paginate($limit, false, ['page' => $start])->toArray();
        $listInfo = $listObj['data'];
        return $this->buildSuccess([
            'list'  => $listInfo,
            'count' => $listObj['total']
        ]);
    }

    /**
     * 新增考试人员
     * @return array
     * @author  winljm001 <winljm001@126.com>
     */
    public function add() {
        $examineeIdArr = explode(',',$this->request->post('examineeId'));
        if ($examineeIdArr[0]== '') {
            return $this->buildFailed(ReturnCode::DB_SAVE_ERROR, '缺少必要参数');
        }
        $examinationRoomId = $this->request->post('examinationRoomId');
        if ($examinationRoomId== '') {
            return $this->buildFailed(ReturnCode::DB_SAVE_ERROR, '缺少必要参数');
        }
        $data = array();
        foreach ($examineeIdArr as $key => $value) {
            $res1 = AdminTestPersonnel::get(['examineeId' => $value, 'examinationRoomId' => $examinationRoomId]);
            if (empty($res1)) {
                $data[]  = [
                    'examineeId' => $value,
                    'updateTime' => time(),
                    'examinationRoomId' => $examinationRoomId
                ];
            }
        }

        $result   =  (new AdminTestPersonnel())->insertAll($data);
        if ($result === false) {
            return $this->buildFailed(ReturnCode::DB_SAVE_ERROR, '操作失败');
        } else {
            return $this->buildSuccess([]);
        }
    }

    /**
     * 移出考试人员
     * @return array
     * @author winljm001 <winljm001@126.com>
     */
    public function del() {
        $examineeIdArr = explode(',',$this->request->post('examineeId'));
        if ($examineeIdArr[0]== '') {
            return $this->buildFailed(ReturnCode::DB_SAVE_ERROR, '缺少必要参数');
        }
        $examinationRoomId = $this->request->post('examinationRoomId');
        if ($examinationRoomId== '') {
            return $this->buildFailed(ReturnCode::DB_SAVE_ERROR, '缺少必要参数');
        }
        $data = array();
        foreach ($examineeIdArr as $key => $value) {
            AdminTestPersonnel::destroy(['examineeId' => $value, 'examinationRoomId' => $examinationRoomId]);
        }
        return $this->buildSuccess([]);
    }

    /**
     * 获取考试人员
     * @return object
     * @author winljm001 <winljm001@126.com>
     */
    public function getDetail() {
        $id = $this->request->get('id');
        if (!$id) {
            return $this->buildFailed(ReturnCode::EMPTY_PARAMS, '缺少必要参数');
        }
        $data = AdminTestPersonnel::get(['id' => $id]);
        return $this->buildSuccess([
            'examinationRoom'  => $data
        ]);
    }

    /**
     * 编辑考试人员
     * @author winljm001 <winljm001@126.com>
     * @return array
     * @throws \think\exception\DbException
     */
    public function edit() {
        $postData = $this->request->post();
        $postData['updateTime'] = time();
        $res = AdminTestPersonnel::update($postData);
        if ($res === false) {
            return $this->buildFailed(ReturnCode::DB_SAVE_ERROR, '操作失败');
        } else {
            return $this->buildSuccess([]);
        }
    }

    /**
     * 考场状态考试人员
     * @return array
     * @author winljm001 <winljm001@126.com>
     */
    public function changeStatus() {
        $idArr = explode(',',$this->request->get('id'));
        if ($idArr[0]== '') {
            return $this->buildFailed(ReturnCode::DB_SAVE_ERROR, '缺少必要参数');
        }
        $status = $this->request->get('status');
        foreach ($idArr as $key => $value) {
            $res = AdminTestPersonnel::update([
                'Id'         => $value,
                'status'     => $status
            ]);
        }
        if ($res === false) {
            return $this->buildFailed(ReturnCode::DB_SAVE_ERROR, '操作失败');
        } else {
            return $this->buildSuccess([]);
        }
    }
    
}
